SERVER_NAME=eureka-server
TAG=1.0
SERVER_PORT=8761

#如果容器存在， 停止容器， 并删除容器
POD_ID=$(docker ps -a | grep "$SERVER_NAME:$TAG" | awk '{print $1}')
if [ -n "$POD_ID" ];
then
  echo "存在容器$SERVER_NAME:$TAG，POD_ID=$POD_ID"
  docker stop $POD_ID
  docker rm $POD_ID
  echo "停止并删除容器$SERVER_NAME:$TAG成功"
fi

#如果存在镜像， 删除镜像
IMAGE_ID=$(docker images $SERVER_NAME:$TAG | grep "$SERVER_NAME" | awk '{print $3}')
if [ -n "$IMAGE_ID" ];
then
  echo "存在镜像$SERVER_NAME:$TAG，IMAGE_ID=$IMAGE_ID"
  docker rmi $IMAGE_ID
  echo "删除镜像$SERVER_NAME:$TAG成功"
fi

docker build -t $SERVER_NAME:$TAG .
echo "构建$SERVER_NAME:$TAG镜像完成"
docker run --name $SERVER_NAME -d -p $SERVER_PORT:$SERVER_PORT $SERVER_NAME:$TAG
echo "$SERVER_NAME容器创建完成"
